package com.ruoyi.web.controller.purchase;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.Brand;
import com.ruoyi.purchase.domain.Firm;
import com.ruoyi.purchase.domain.Product;
import com.ruoyi.purchase.domain.Type;
import com.ruoyi.purchase.service.BrandService;
import com.ruoyi.purchase.service.FirmService;
import com.ruoyi.purchase.service.ProductService;
import com.ruoyi.purchase.service.TypeService;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping("purchase/product")
public class ProductController extends BaseController {
    private String prefix = "purchase/product";
    @Autowired
    private ProductService productService;

    @Autowired
    private FirmService firmService;
    @Autowired
    private BrandService brandService;

    @Autowired
    private TypeService typeService;


    /**
     * 根据品牌id类型id查询型号
     * @return
     */
    @GetMapping("/productModelList")
    @ResponseBody
    public List<Product> productModelList(Integer brandId,Integer typeId){
        System.out.println("品牌Id："+brandId+"类型Id："+typeId);
        Product product=new Product();
        product.setPoBrandId(brandId);
        product.setPoTypeId(typeId);
        List<Product> productList=productService.findAllProduct(product);
        for (Product product1 : productList) {
            System.out.println("-------------"+product1);
        }
        return productList;
    }

    @RequiresPermissions("purchase:product:view")
    @GetMapping()
    public String Product(Model model)
    {

        List<Firm> firmList = firmService.list();
        List<Type> typeList = typeService.list();
        List<Brand> brandList = brandService.list();
        model.addAttribute("brandList",brandList);
        model.addAttribute("firmList",firmList);
        model.addAttribute("typeList",typeList);
        return prefix + "/product";
    }
    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("purchase:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Product product)
    {

        startPage();
        List<Product> list = productService.queryList(product);
        return getDataTable(list);
    }

    /**
     * 导出厂商信息列表
     */
    @RequiresPermissions("purchase:product:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Product product)
    {
        List<Product> list = productService.queryList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 增加厂商信息
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<Firm> firmList = firmService.list();
        List<Type> typeList = typeService.list();
        List<Brand> brandList = brandService.list();
        model.addAttribute("brandList",brandList);
        model.addAttribute("firmList",firmList);
        model.addAttribute("typeList",typeList);
        return prefix + "/add";
    }

    /**
     * 新增保存厂商信息
     */
    @RequiresPermissions("purchase:product:add")
    @Log(title = "厂商信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Product product) { return toAjax(productService.save(product)); }

    /**
     * 修改厂商信息
     */
    @GetMapping("/edit/{pid}")
    public String edit(@PathVariable("pid") Integer productId, ModelMap mmap)
    {
        Product product = productService.getById(productId);
        SysUser user = getSysUser();//当前登录用户信息
        List<Firm> firmList = firmService.list();
        List<Type> typeList = typeService.list();
        List<Brand> brandList = brandService.list();
        mmap.addAttribute("brandList",brandList);
        mmap.put("user",user);
        mmap.addAttribute("firmList",firmList);
        mmap.addAttribute("typeList",typeList);
        mmap.put("product", product);
        return prefix + "/edit";
    }

    /**
     * 修改保存厂商信息
     */
    @RequiresPermissions("purchase:product:edit")
    @Log(title = "品牌信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Product product)
    {
        return toAjax(productService.updateById(product));
    }

    /**
     * 删除厂商信息
     */
    @RequiresPermissions("purchase:product:remove")
    @Log(title = "品牌信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(productService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 用户状态修改
     */
    @Log(title = "厂商管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("purchase:product:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(Product product)
    {
        return toAjax(productService.changeStatus(product));
    }

    /**
     * 详情
     * @param productId
     * @param mmap
     * @return
     */
    @RequiresPermissions("purchase:product:detail")
    @GetMapping("/detail/{productId}")
    public String detail(@PathVariable("productId") Integer productId, ModelMap mmap)
    {
        List<Firm> firmList = firmService.list();
        List<Type> typeList = typeService.list();
        SysUser user = getSysUser();//当前登录用户信息
        List<Brand> brandList = brandService.list();
        mmap.addAttribute("brandList",brandList);
        mmap.put("user",user);
        mmap.addAttribute("firmList",firmList);
        mmap.addAttribute("typeList",typeList);
        mmap.put("product", productService.selectProductById(productId));
        return prefix + "/detail";
    }

    /**
     * 获取厂商
     * @return
     */
    @GetMapping("/firm/select")
    @ResponseBody
    public List<Firm> selectFirm()
    {
        List<Firm> list = productService.selectFirm();
        return list;
    }

    /**
     * 获取品牌
     * @return
     */
    @GetMapping("/brand/select")
    @ResponseBody
    public List<Brand> selectBrand()
    {
        List<Brand> list = productService.selectBrand();
        return list;
    }

    /**
     * 获取类型
     * @return
     */
    @GetMapping("/type/select")
    @ResponseBody
    public List<Type> selectType()
    {
        List<Type> list = productService.selectType();
        return list;
    }


}
